package patrol.mobile.com.mobilepatrol.activity.office;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.adapter.DepartmentExaminationDetailsAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DepartmentExaminationDetailsBean;
import patrol.mobile.com.mobilepatrol.bean.KeShiListBean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.StringUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by K on 2018/9/18.
 * des: 各科室考核明细
 */
public class DepartmentExaminationDetailsActivity extends CommonListActivity {

    private static final String ORG_ID = "ORG_ID";
    private static final String FROM_WHAT = "FROM_WHAT";
    private static final String TITLE = "TITLE";
    private String fromWhat;//1是收费服务区 2是分局
    private String orgId;
    private int page = 1;
    private List<DepartmentExaminationDetailsBean> departmentExaminationDetailsBeans;
    private List<DepartmentExaminationDetailsBean> departmentExaminationDetailsBeans1;
    private String title;

    public static void show(Context context, String fromWhat, String id, String title) {
        Intent intent = new Intent(context, DepartmentExaminationDetailsActivity.class);
        intent.putExtra(FROM_WHAT, fromWhat);
        intent.putExtra(ORG_ID, id);
        intent.putExtra(TITLE, title);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        super.initView();
        fromWhat = getIntent().getStringExtra(FROM_WHAT);
        orgId = getIntent().getStringExtra(ORG_ID);
        title = getIntent().getStringExtra(TITLE);
        if (!StringUtils.isEmpty(title)){
            tv_center_title.setText(title);
        }
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new DepartmentExaminationDetailsAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return "各科室考核明细";
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
        if ("1".equals(fromWhat)) {
            requestData1(true);
        } else {
            requestData2(true);
        }
    }

    /**
     * 科室对收费服务区
     */
    private void requestData1(final boolean isRefresh) {
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        HttpSender sender = new HttpSender(this, baseUrl, Urls.ORG_SERVICE_AREA_LIST);
        sender.requestPost("orgCheckAreaId", orgId, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();

                JsonArray listArray = jo.getAsJsonArray("list");
                int rows = jo.get("rows").getAsInt();
                departmentExaminationDetailsBeans = GsonUtil.jsonToList(listArray.toString(), DepartmentExaminationDetailsBean.class);
                setData(page, isRefresh, departmentExaminationDetailsBeans, rows);
            }
        });
    }

    /**
     * 科室对分局
     */
    private void requestData2(final boolean isRefresh) {
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        HttpSender sender = new HttpSender(this, baseUrl, Urls.ORG_SUB_BUREAU_LIST);
        sender.requestPost("checkOrgId", orgId, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();

                JsonArray listArray = jo.getAsJsonArray("list");
                int rows = jo.get("rows").getAsInt();
                departmentExaminationDetailsBeans1 = GsonUtil.jsonToList(listArray.toString(), DepartmentExaminationDetailsBean.class);
                setData(page, isRefresh, departmentExaminationDetailsBeans1, rows);
            }
        });
    }

    @Override
    public void setListener() {
        super.setListener();
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (fromWhat.equals("1")) {
                    DeductionActivity.show(DepartmentExaminationDetailsActivity.this, 4, departmentExaminationDetailsBeans.get(position).getId());
                } else if (fromWhat.equals("2")) {
                    DeductionActivity.show(DepartmentExaminationDetailsActivity.this, 3, departmentExaminationDetailsBeans1.get(position).getId());
                }
            }
        });
    }
}
